﻿// CSelectInject.cpp: 实现文件
//

#include "pch.h"
#include "Injector.h"
#include "CSelectInject.h"
#include "afxdialogex.h"
#include "CtrlCode.h"


// CSelectInject 对话框

IMPLEMENT_DYNAMIC(CSelectInject, CDialogEx)

CSelectInject::CSelectInject(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SELECTINJECT_DIALOG, pParent)
{

}

CSelectInject::~CSelectInject()
{
    m_EditFunction.DestroyWindow();
    m_StaticFunctionTip.DestroyWindow();
}

void CSelectInject::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO_INJECT_TYPE, m_ComboInjectType);
}


BEGIN_MESSAGE_MAP(CSelectInject, CDialogEx)
    ON_BN_CLICKED(IDC_BTN_INJECTCONFIRM, &CSelectInject::OnBnClickedBtnInjectconfirm)
    ON_WM_CLOSE()
    ON_CBN_SELCHANGE(IDC_COMBO_INJECT_TYPE, &CSelectInject::OnCbnSelchangeComboInjectType)
END_MESSAGE_MAP()


// CSelectInject 消息处理程序


BOOL CSelectInject::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    //
    //  初始化下拉框
    //

    m_ComboInjectType.AddString(_T("远线程注入"));
    m_ComboInjectType.AddString(_T("APC 注入"));
    m_ComboInjectType.AddString(_T("劫持注入"));
    m_ComboInjectType.AddString(_T("消息钩子注入"));
    m_ComboInjectType.AddString(_T("PE文件注入"));
    m_ComboInjectType.SetCurSel(0);

    //
    //  初始化一些字体、控件
    //

    m_FunctionFont.DeleteObject();
    m_FunctionFont.CreatePointFont(100, _T("微软雅黑"));
    m_TipFont.DeleteObject();
    m_TipFont.CreatePointFont(120, _T("微软雅黑"));

    m_EditFunction.Create(WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | WS_BORDER, CRect(10, 40, 145, 65), this, IDC_EDIT_HOOKPROC);
    m_EditFunction.SetFont(&m_FunctionFont);
    m_EditFunction.ShowWindow(SW_HIDE);
    m_EditFunction.UpdateWindow();

    m_StaticFunctionTip.Create(_T("HookProc"), WS_VISIBLE | WS_CHILD, CRect(165, 40, 300, 65), this, IDC_STATIC_HOOKPROC);
    m_StaticFunctionTip.SetFont(&m_TipFont);
    m_StaticFunctionTip.ShowWindow(SW_HIDE);
    m_StaticFunctionTip.UpdateWindow();

    return TRUE;
}

BOOL CSelectInject::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        switch (pMsg->wParam)
        {
        case VK_ESCAPE:
            return TRUE;
        case VK_RETURN:
            return TRUE;
        default:
            break;
        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);
}

void CSelectInject::OnBnClickedBtnInjectconfirm()
{
    PINJECT_CONFIG pConfig = (PINJECT_CONFIG)new char[sizeof(INJECT_CONFIG)];
    CString strFunction;
    PWCHAR pWch;
    int nLen;
    PCHAR pBuff = nullptr;

    if (pConfig)
    {
        RtlZeroMemory(pConfig, sizeof(INJECT_CONFIG));
        pConfig->bConfirm = TRUE;
        pConfig->Type = (MODULE_INJECT)m_ComboInjectType.GetCurSel();

        if (pConfig->Type == M_MESSAGE_HOOK)
        {
            m_EditFunction.GetWindowTextW(strFunction);
            pWch = strFunction.GetBuffer(0);
            nLen = strFunction.GetLength();
            pBuff = new char[nLen];
            if (!pBuff) goto _None;
            WideCharToMultiByte(CP_ACP, 0, pWch, -1, pBuff, nLen, NULL, FALSE);
            pConfig->Data = pBuff;
            pConfig->DataLen = nLen;
        }

        ::SendMessage(this->GetParent()->m_hWnd, WM_MODULEINJECT, (WPARAM)pConfig, 0);

        if (pBuff)
            delete[] pBuff;

        delete[] pConfig;
    }
    else
    {
_None:
        ::SendMessage(this->GetParent()->m_hWnd, WM_MODULEINJECT, 0, 0);
    }

    EndDialog(0);
}


void CSelectInject::OnClose()
{
    ::SendMessage(this->GetParent()->m_hWnd, WM_MODULEINJECT, 0, 0);
    CDialogEx::OnClose();
}



void CSelectInject::OnCbnSelchangeComboInjectType()
{
    int nSel = m_ComboInjectType.GetCurSel();

    if (nSel == M_MESSAGE_HOOK)
    {
        m_EditFunction.ShowWindow(SW_SHOW);
        m_EditFunction.UpdateWindow();
        m_StaticFunctionTip.ShowWindow(SW_SHOW);
        m_StaticFunctionTip.UpdateWindow();
    }
    else
    {
        m_EditFunction.ShowWindow(SW_HIDE);
        m_EditFunction.UpdateWindow();
        m_StaticFunctionTip.ShowWindow(SW_HIDE);
        m_StaticFunctionTip.UpdateWindow();
    }
}
